package com.leetcode.No1790;

import org.junit.Test;

/**
 * @program: Solution
 * @description: 仅执行一次字符串交换能否使两个字符串相等
 * @author: Wang Zhihua
 * @date: 2022-10-11
 */
public class Solution {
    public boolean areAlmostEqual(String s1, String s2) {
        int diffCount = 0;
        char[] diffChar1 = new char[3];
        char[] diffChar2 = new char[3];
        int len = s1.length();
        for (int i = 0; i < len; ++i) {
            if (s1.charAt(i) != s2.charAt(i)) {
                diffChar1[diffCount] = s1.charAt(i);
                diffChar2[diffCount] = s2.charAt(i);
                ++diffCount;
            }
            if (diffCount > 2) {
                return false;
            }
        }
        return (diffCount == 0 || diffCount == 2) && ((diffChar1[0] ^ diffChar2[1]) + (diffChar1[1] ^ diffChar2[0]) == 0);
    }

    @Test
    public void test1() {
        String s1 = "bank";
        String s2 = "kanb";
        System.out.println(areAlmostEqual(s1, s2));

        s1 = "attack";
        s2 = "defend";
        System.out.println(areAlmostEqual(s1, s2));

        s1 = "kelb";
        s2 = "kelb";
        System.out.println(areAlmostEqual(s1, s2));

        s1 = "abcd";
        s2 = "dcba";
        System.out.println(areAlmostEqual(s1, s2));

        s1 = "caa";
        s2 = "aaz";
        System.out.println(areAlmostEqual(s1, s2));

        s1 = "aa";
        s2 = "bb";
        System.out.println(areAlmostEqual(s1, s2));
    }
}
